package v1

import (
	"github.com/360EntSecGroup-Skylar/excelize"
	"github.com/gin-gonic/gin"
	"github.com/spf13/cast"
	"hr/api/dto"
	"hr/api/middleware"
	"hr/db"
	"hr/utils"
	"strconv"
)

// ExportExcel
// @Summary 导出Excel
// @Tags 考勤管理相关api
// @Accept  json
// @Produce json
// @Param Access-Token header string true "Authentication header"
// @Param no query string false "员工号"
// @Param dept query string false "所属组织"
// @Param name query string false "姓名"
// @Success 200 {object} utils.JsonContent
// @Failure 400 {object} utils.JsonContent
// @Failure 500 {object} utils.JsonContent
// @Router /api/v1/auth/attendance/export [get]
func ExportExcel(c *gin.Context) {
	no := c.Query("no")
	dept := c.Query("dept")
	name := c.Query("name")
	startDate := c.Query("startDate")
	endDate := c.Query("endDate")

	attendanceManageList, err := db.ExportAttendance(no, dept, name, startDate, endDate)
	if err != nil {
		utils.EchoError(c, err.Error())
		return
	}

	excel := excelize.NewFile()
	sheetName := "考勤数据"
	sheet := excel.NewSheet(sheetName)
	excel.SetCellValue(sheetName, "A1", "员工姓名")
	excel.SetCellValue(sheetName, "B1", "员工号")
	excel.SetCellValue(sheetName, "C1", "所属组织")
	excel.SetCellValue(sheetName, "D1", "考勤类型")
	excel.SetCellValue(sheetName, "E1", "考勤时间")

	for i, attendance := range attendanceManageList {
		excel.SetCellValue(sheetName, "A"+strconv.Itoa(i+2), attendance.NickName)
		excel.SetCellValue(sheetName, "B"+strconv.Itoa(i+2), attendance.No)
		excel.SetCellValue(sheetName, "C"+strconv.Itoa(i+2), attendance.DeptName)
		excel.SetCellValue(sheetName, "D"+strconv.Itoa(i+2), attendance.AttendanceType)
		excel.SetCellValue(sheetName, "E"+strconv.Itoa(i+2), attendance.CreatedAt)
	}
	excel.SetActiveSheet(sheet)
	filePath := "test.xlsx"
	if err := excel.SaveAs(filePath); err != nil {
		utils.EchoError(c, err.Error())
		return
	}
	c.Header("Content-Disposition", "attachment;filename=abc.xlsx")
	c.Header("Cache-Control", "no-cache")
	c.File(filePath)
}

// GetAttendanceAll
// @Summary 获取所有考勤记录
// @Tags 考勤管理相关api
// @Accept  json
// @Produce json
// @Param Access-Token header string true "Authentication header"
// @Param pageNo query string true "页码"
// @Param pageSize query string true "页大小"
// @Param no query string false "员工号"
// @Param dept query string false "所属组织"
// @Param name query string false "姓名"
// @Success 200 {object} utils.JsonContent
// @Failure 400 {object} utils.JsonContent
// @Failure 500 {object} utils.JsonContent
// @Router /api/v1/auth/attendance/all [get]
func GetAttendanceAll(c *gin.Context) {
	pageNoStr := c.Query("pageNo")
	pageSizeStr := c.Query("pageSize")
	no := c.Query("no")
	dept := c.Query("dept")
	name := c.Query("name")
	startDate := c.Query("startDate")
	endDate := c.Query("endDate")

	var pageNo, pageSize int
	if !utils.IsStringEmpty(pageNoStr) {
		pageNo = cast.ToInt(pageNoStr)
	}
	if !utils.IsStringEmpty(pageSizeStr) {
		pageSize = cast.ToInt(pageSizeStr)
	}
	paging := &dto.Pagination{
		PageNo:   pageNo,
		PageSize: pageSize,
	}

	a, err := db.GetAttendanceManagePaging(*paging, no, dept, name, startDate, endDate)

	if err != nil {
		utils.EchoError(c, err.Error())
		return
	}

	utils.EchoSuccess(c, a, "请求成功")
	return
}

// GetAttendanceUser
// @Summary 获取用户考勤记录
// @Tags 考勤管理相关api
// @Accept  json
// @Produce json
// @Param Access-Token header string true "Authentication header"
// @Param pageNo query string true "页码"
// @Param pageSize query string true "页大小"
// @Success 200 {object} utils.JsonContent
// @Failure 400 {object} utils.JsonContent
// @Failure 500 {object} utils.JsonContent
// @Router /api/v1/auth/attendance/user [get]
func GetAttendanceUser(c *gin.Context) {
	pageNoStr := c.Query("pageNo")
	pageSizeStr := c.Query("pageSize")

	var pageNo, pageSize int
	if !utils.IsStringEmpty(pageNoStr) {
		pageNo = cast.ToInt(pageNoStr)
	}
	if !utils.IsStringEmpty(pageSizeStr) {
		pageSize = cast.ToInt(pageSizeStr)
	}
	paging := &dto.Pagination{
		PageNo:   pageNo,
		PageSize: pageSize,
	}
	id, _ := middleware.GetCurrentUserId(c)
	a, err := db.GetAttendanceManageByUserPaging(*paging, cast.ToString(id))
	if err != nil {
		utils.EchoError(c, err.Error())
		return
	}

	utils.EchoSuccess(c, a, "请求成功")
	return
}

// AttendanceSubmit
// @Summary 提交用户考勤记录
// @Tags 考勤管理相关api
// @Accept  json
// @Produce json
// @Param Access-Token header string true "Authentication header"
// @Param AttendanceRequest body dto.AttendanceRequest true "信息"
// @Success 200 {object} utils.JsonContent
// @Failure 400 {object} utils.JsonContent
// @Failure 500 {object} utils.JsonContent
// @Router /api/v1/auth/attendance/submit [post]
func AttendanceSubmit(c *gin.Context) {
	p := &dto.AttendanceRequest{}
	if err := c.ShouldBindJSON(p); err != nil {
		utils.EchoError(c, "缺少必要参数信息")
		return
	}

	id, _ := middleware.GetCurrentUserId(c)
	a, err := db.AttendanceSubmit(*p, id)
	if err != nil {
		utils.EchoError(c, err.Error())
		return
	}

	utils.EchoSuccess(c, a, "创建成功")
	return
}
